﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDoc.Controls.Device
{
    public partial class ucDeviceList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            DataTable dt = new DataTable();
            dt = BO.Data.Select_Devices_All_by_Email(txtEmail.Text.Trim());
            grdReportDeviceActive.DataSource = dt;
            grdReportDeviceActive.DataBind();
        }

        protected void btView_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void repDevices_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //string username = "";
            if (e.CommandName == "deactive")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                BO.Data.Update_Device_Active(id,false);
                BindData();
            }
            else if (e.CommandName == "active")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                BO.Data.Update_Device_Active(id, true);
                BindData();
            }
            else if (e.CommandName == "save")
            {
                /*
                HtmlInputHidden t = e.Item.FindControl("firstNameHidden") as HtmlInputHidden;
                Data.Contacts[e.Item.ItemIndex].FirstName = t.Value;
                t = e.Item.FindControl("lastNameHidden") as HtmlInputHidden;
                Data.Contacts[e.Item.ItemIndex].LastName = t.Value;
                EditIndex = -1;
                */
            }
        }

        protected bool ReturnVisible(string active, string item)
        {
            if (Convert.ToBoolean(active) && item == "1")
                return true;
            else if (!Convert.ToBoolean(active) && item == "2")
                return true;
            else
                return false;
        }

        protected string LastDateSync (string datetime)
        {
            if(!string.IsNullOrEmpty(datetime))
            {
                return Convert.ToDateTime(datetime).ToString("dd/MM/yyyy HH:mm");
            }
            else
            {
                return "";
            }
        }

        protected void grdReportDeviceActive_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdReportDeviceActive.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void grdReportDeviceActive_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //string username = "";
            if (e.CommandName == "deactive")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                BO.Data.Update_Device_Active(id, false);
                BindData();
            }
            else if (e.CommandName == "active")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                BO.Data.Update_Device_Active(id, true);
                BindData();
            }
            else if (e.CommandName == "save")
            {
                /*
                HtmlInputHidden t = e.Item.FindControl("firstNameHidden") as HtmlInputHidden;
                Data.Contacts[e.Item.ItemIndex].FirstName = t.Value;
                t = e.Item.FindControl("lastNameHidden") as HtmlInputHidden;
                Data.Contacts[e.Item.ItemIndex].LastName = t.Value;
                EditIndex = -1;
                */
            }
        }

        protected string ReturnActive(string value)
        {
            if(Convert.ToBoolean(value))
            {
                return "<img src='/images/true.png' height='24px' width='24px'></img>";
            }
            else
            {
                return "<img src='/images/false.png' height='24px' width='24px'></img>";
            }
        }
    }
}